﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace WebsiteBanSachOnline
{
    public partial class TimKiemSach : System.Web.UI.Page
    {
        private int sosachtrentrang = 3;
        //var
        int trang;
        string loai;
        string maDanhMuc;
        bool isOke;
        string giatri="";
        //
        protected void Page_Load(object sender, EventArgs e)
        {


            BookOnlineDataContext book = new BookOnlineDataContext();
            if (!IsPostBack)
            {

                var dsdm = from dm in book.DanhMucs
                           select new { dm.MaDanhMuc, dm.TenDanhMuc };
                bllVerticalMenu.Items.Clear();
                bllVerticalMenu.DisplayMode = BulletedListDisplayMode.HyperLink;
                foreach (var obj in dsdm)
                {
                    ListItem li = new ListItem(obj.TenDanhMuc, "~/TimKiemSach.aspx?Loai=DanhMuc&MaDanhMuc="+obj.MaDanhMuc);
                    bllVerticalMenu.Items.Add(li);
                }
                FillDataList();
            }

            //get parameter
            if (Request.QueryString["Trang"] != null) 
            {
                trang = Int32.Parse(Request.QueryString["Trang"].ToString());
            }
            else {
                trang = 1;
            }

             if (Request.QueryString["Loai"] != null) 
            {
                loai = Request.QueryString["Loai"].ToString();
                Response.Write(loai);
                isOke = true;
            }
            else {
                loai = "";
            }
            //
           
             if (Request.QueryString["MaDanhMuc"] != null) 
            {
                maDanhMuc = Request.QueryString["MaDanhMuc"].ToString();
            }
           //lay gia tri
             if (Request.QueryString["GiaTri"] != null)
             {
                 giatri = Request.QueryString["GiaTri"].ToString();
             }
             else
             {
                 giatri = "";
             }
          
           
           
        }
        private void FillDataList()
        {
            String connectionString = WebConfigurationManager.ConnectionStrings["BookOnlineConnectionString"].ConnectionString;
            string sql = "select * from Sach";
            SqlConnection connection = new SqlConnection(connectionString);
            SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
            DataTable table = new DataTable();
            adapter.Fill(table);
            DataList1.DataSource = table;
            DataList1.DataBind();
        }
        private bool TimKiem(string source,string val) {
            string[] s = val.Split(new string[] { " ", "," },StringSplitOptions.RemoveEmptyEntries);
           
            foreach (string item in s)
            {
                if (source.Contains(item)) 
                {
                    return true;
                }
            }
            return false;
        }
        protected void hlTrang_Click(object sender, EventArgs e)
        {
            LinkButton bt = (LinkButton)sender;
            bt.Attributes.Add("font-weight", "bolder");
            bt.Attributes.Add("font-size", "14px");
            bt.Enabled = false;
            int trang = int.Parse(bt.Text);
            Response.Redirect(String.Format("~/TimKiemSach.aspx?Loai="+loai+"&Trang="+trang+"&GiaTri="+giatri));

        }
        public void showBooks(List<Sach> list, int trang)
        {

            Table tb = new Table();
            tb.ID = "Result";

            if (list.Count == 0)
            {
                TableRow tr = new TableRow();
                TableCell cell = new TableCell();
                cell.Attributes.Add("style", "text-align:left;padding:10px;font-weight:bold;");
                cell.Text = "Tìm thấy 0 kết quả";
                tr.Cells.Add(cell);
                tb.Rows.Add(tr);
            }
            else
            {
                //khoi tao header;


                TableRow tr1 = new TableRow();
                TableCell cell1 = new TableCell();
                cell1.Attributes.Add("style", "text-align:left;padding:10px;font-weight:bolder;color:#FF0000;border-bottom:4px solid black;");
                cell1.Text = "Tìm thấy " + list.Count + " kết quả";
                tr1.Cells.Add(cell1);
                tb.Rows.Add(tr1);
                //phan trang


                for (int i = (trang - 1) * sosachtrentrang; i < (trang) * sosachtrentrang; i++)
                {
                    TableRow tr = new TableRow();
                    int j;
                    for (j = i; j < i + 3 && j < list.Count; j++)
                    {
                        Sach s = list.ElementAt(j);
                        TableCell cell = new TableCell();

                        cell.Attributes.Add("style", "width:233px;");
                        //do st
                        UCSach ucs = (UCSach)LoadControl("~/UCSach.ascx");
                        ucs.TenSach = s.TenSach;
                        ucs.Gia = ((double)s.GiaBan).ToString("#,##0") + " VND";
                        ucs.LinkHinhAnh = "~/" + s.HinhAnh;
                        ucs.LinkChiTiet = "fsd";
                        ucs.LinkThemVaoGio = "fasdfd";
                        cell.Controls.Add(ucs);
                        tr.Cells.Add(cell);
                    }
                    if (j == trang * sosachtrentrang && j != i + 3)
                    {
                        TableCell cell = new TableCell();
                        cell.Text = "";
                        cell.Attributes.Add("style", "width:auto;");
                        tr.Cells.Add(cell);
                    }
                    tb.Rows.Add(tr);
                    i += 2;
                }
                TableRow tr2 = new TableRow();
                TableCell cell2 = new TableCell();
                cell2.Attributes.Add("style", "border-top:2px solid black;width:100%;");
                int sotrang = (list.Count % sosachtrentrang != 0) ? list.Count / sosachtrentrang + 1 : list.Count() / sosachtrentrang;
                Label lbphantrang = new Label();
                lbphantrang.Attributes.Add("style", "text-align:left;padding-left:10px;float:left;");
                Label text = new Label();
                text.Text = "Trang ";
                lbphantrang.Controls.Add(text);
                for (int i = 1; i < sotrang + 1; i++)
                {
                    LinkButton hl = new LinkButton();
                    hl.Text += i.ToString();
                    hl.ID = "LB_" + i.ToString();
                    hl.Click += new EventHandler(hlTrang_Click);
                    lbphantrang.Controls.Add(hl);
                    if (i != sotrang)
                    {
                        Label lb1 = new Label();
                        lb1.Text = " | ";
                        lbphantrang.Controls.Add(lb1);
                    }

                }
                cell2.Controls.Add(lbphantrang);
                cell2.ColumnSpan = 3;
                tr2.Cells.Add(cell2);
                tb.Rows.Add(tr2);
            }
            this.right_panel.Controls.Add(tb);
        }
    }
}